package com.hl.card.modular.pack.controller;

import com.google.code.kaptcha.Constants;
import com.hl.card.common.controller.BaseController;
import com.hl.card.common.exception.InvalidKaptchaException;
import com.hl.card.common.node.MenuNode;
import com.hl.card.common.persistence.dao.UserMapper;
import com.hl.card.common.persistence.model.User;
import com.hl.card.common.util.HttpUtils;
import com.hl.card.common.util.ResponseObject;
import com.hl.card.core.log.LogManager;
import com.hl.card.core.log.factory.LogTaskFactory;
import com.hl.card.core.shiro.ShiroKit;
import com.hl.card.core.shiro.ShiroUser;
import com.hl.card.core.util.ToolUtil;
import com.hl.card.modular.system.dao.MenuDao;
import com.hl.card.modular.system.service.ISysDicService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.hl.card.core.support.HttpKit.getIp;

/**
 * 登录控制器
 *
 * @author fengshuonan
 * @Date 2017年1月10日 下午8:25:24
 */
@RestController
public class CommonController extends BaseController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    ISysDicService sysDicService;

    /**
     * 获取H5版本号
     */
    @RequestMapping(value = "/version/get", method = RequestMethod.GET)
    public ResponseObject version(@RequestParam String client) {
        return getVersion(client);
    }

    /**
     * 获取H5版本号
     */
    @RequestMapping(value = "/version/H5", method = RequestMethod.GET)
    public ResponseObject getH5Version() {
        return getVersion("H5");
    }

    /**
     * 获取ANDROID版本号
     */
    @RequestMapping(value = "/version/android", method = RequestMethod.GET)
    public ResponseObject getAndroidVersion() {
        return getVersion("ANDROID");
    }

    /**
     * 获取IOS版本号
     */
    @RequestMapping(value = "/version/ios", method = RequestMethod.GET)
    public ResponseObject getIosVersion() {
        return getVersion("IOS");
    }

    /**
     * 获取APP版本号
     */
    @RequestMapping(value = "/version/app", method = RequestMethod.GET)
    public ResponseObject getAppVersion() {
        return getVersion();
    }

    /**
     * 进入H5最新版本
     */
    @RequestMapping(value = "/home.html", method = RequestMethod.GET)
    public void getH5Version(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
        String home = getVersionStr("HOME_URL");
        try {
            response.sendRedirect(home + "?" + System.currentTimeMillis());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取来访IP
     */
    @RequestMapping(value = "/getip", method = RequestMethod.GET)
    public String getIp(HttpServletRequest request) {
        return HttpUtils.getIpAddr(request);
    }

    private String getVersionStr(String plantform) {
        ResponseObject res = new ResponseObject();
        res.setErroCode(1);
        res.setData("版本信息不存在");
        Map map=new HashMap<>();
        String version = "https://m.bibi321.com";
//        String version = stringRedisTemplate.opsForValue().get("VERSION" + ":" + plantform);
//        if(StringUtils.isNotBlank(version)) {
//            res.setErroCode(0);
//            map.put("version", version);
//            map.put("msg", "版本获取成功");
//            res.setData(map);
//        }else {
        Map<String, Object> item = sysDicService.selectListByCodeNo("VERSION", plantform);
        if(item != null) {
            version = (String) item.get("dicValue");
        }
        return version;
    }

    private ResponseObject getVersion(String plantform) {
        ResponseObject res = new ResponseObject();
        res.setErroCode(1);
        res.setData("版本信息不存在");
        Map map=new HashMap<>();
        String version;
//        String version = stringRedisTemplate.opsForValue().get("VERSION" + ":" + plantform);
//        if(StringUtils.isNotBlank(version)) {
//            res.setErroCode(0);
//            map.put("version", version);
//            map.put("msg", "版本获取成功");
//            res.setData(map);
//        }else {
            Map<String, Object> item = sysDicService.selectListByCodeNo("VERSION", plantform);
            if(item != null) {
                version = (String) item.get("dicValue");
                if(StringUtils.isNotBlank(version)) {
                    res.setErroCode(0);
                    map.put("version", version);
                    map.put("msg", "版本获取成功");
                    res.setData(map);
                }
            }
//        }
        return res;
    }

    private ResponseObject getVersion() {
        ResponseObject res = new ResponseObject();
        res.setErroCode(1);
        res.setData("版本信息不存在");
        Map map=new HashMap<>();
        String version;
        Map<String, Object> item = sysDicService.selectListByCodeNo("VERSION", "ANDROID");
        if(item != null) {
            version = (String) item.get("dicValue");
            if(StringUtils.isNotBlank(version)) {
                res.setErroCode(0);
                map.put("android_version", version);
            }
        }
        item = sysDicService.selectListByCodeNo("VERSION", "ANDROID_URL");
        if(item != null) {
            version = (String) item.get("dicValue");
            if(StringUtils.isNotBlank(version)) {
                res.setErroCode(0);
                map.put("android_url", version);
            }
        }
        item = sysDicService.selectListByCodeNo("VERSION", "ANDROID_DETAIL");
        if(item != null) {
            version = (String) item.get("dicValue");
            if(StringUtils.isNotBlank(version)) {
                res.setErroCode(0);
                map.put("android_detail", version);
            }
        }
        item = sysDicService.selectListByCodeNo("VERSION", "IOS");
        if(item != null) {
            version = (String) item.get("dicValue");
            if(StringUtils.isNotBlank(version)) {
                res.setErroCode(0);
                map.put("ios_version", version);
            }
        }
        item = sysDicService.selectListByCodeNo("VERSION", "IOS_URL");
        if(item != null) {
            version = (String) item.get("dicValue");
            if(StringUtils.isNotBlank(version)) {
                res.setErroCode(0);
                map.put("ios_url", version);
            }
        }
        item = sysDicService.selectListByCodeNo("VERSION", "IOS_DETAIL");
        if(item != null) {
            version = (String) item.get("dicValue");
            if(StringUtils.isNotBlank(version)) {
                res.setErroCode(0);
                map.put("ios_detail", version);
            }
        }
        res.setData(map);
        return res;
    }

    private ResponseObject getDicValue(String Code, String No) {
        ResponseObject res = new ResponseObject();
        res.setErroCode(1);
        res.setData("信息不存在");
        Map map=new HashMap<>();
        String value;
//        String version = stringRedisTemplate.opsForValue().get("VERSION" + ":" + plantform);
//        if(StringUtils.isNotBlank(version)) {
//            res.setErroCode(0);
//            map.put("version", version);
//            map.put("msg", "版本获取成功");
//            res.setData(map);
//        }else {
        Map<String, Object> item = sysDicService.selectListByCodeNo(Code, No);
        if(item != null) {
            value = (String) item.get("dicValue");
            if(StringUtils.isNotBlank(value)) {
                res.setErroCode(0);
                map.put("value", value);
                map.put("msg", "获取成功");
                res.setData(map);
            }
        }
//        }
        return res;
    }

    /**
     * 获取充值转账钱包地址
     */
    @RequestMapping(value = "/recharge/token", method = RequestMethod.GET)
    public ResponseObject getRechargeToken() {
        return getDicValue("TZCZPZ", "ZZDZ");
    }

    /**
     * 获取兑换面值列表
     */
    @RequestMapping(value = "/exch/getRate", method = RequestMethod.GET)
    public ResponseObject getRate() {
        return getDicValue("BDDHPZ", "EXCHRATE");
    }

    /**
     * 获取兑换面值列表
     */
    @RequestMapping(value = "/exch/getList", method = RequestMethod.GET)
    public ResponseObject getExchList() {
        return getList();
    }

    /**
     * 获取兑换面值列表
     */
    @RequestMapping(value = "/version/getList", method = RequestMethod.GET)
    public ResponseObject getAvailableList() {
        return getList();
    }

    private ResponseObject getList() {
        ResponseObject res = new ResponseObject();
        res.setErroCode(1);
        res.setData("面值列表不存在");
        Map map=new HashMap<>();
        String version;
//        String version = stringRedisTemplate.opsForValue().get("VERSION" + ":" + plantform);
//        if(StringUtils.isNotBlank(version)) {
//            res.setErroCode(0);
//            map.put("version", version);
//            map.put("msg", "版本获取成功");
//            res.setData(map);
//        }else {
        List<Map<String, Object>> list = sysDicService.selectListByCode("BDDHMZ");
        List<JSONObject> resList = new ArrayList<>();
        String temp;
        if(list != null) {
            for (Map<String, Object> item : list) {
                temp = (String) item.get("dicValue");
                int i = Integer.parseInt(temp);
                if (i > 0) {
                    JSONObject obj = new JSONObject();
                    obj.put("code", item.get("dicNo"));
                    obj.put("value", temp);
                    resList.add(obj);
                }
            }
            res.setErroCode(0);
            map.put("list", resList);
            map.put("msg", "面值列表获取成功");
            res.setData(map);
        }
        return res;
    }
}
